local base = require "base"
local msghelper = require "tablehelper"
local timetool = require "timetool"
local timer = require "timer"
local filelog = require "filelog"
local logicmng = require "logicmng"
local gamelog = require "gamelog"
require "enum"
local RoomGameLogic = {}

function RoomGameLogic.init(gameobj, tableobj)
	gameobj.tableobj = tableobj
	gameobj.stateevent[ETableState.TABLE_STATE_GAME_START] = RoomGameLogic.gamestart
	gameobj.stateevent[ETableState.TABLE_STATE_ONE_GAME_START] = RoomGameLogic.onegamestart
	gameobj.stateevent[ETableState.TABLE_STATE_ONE_GAME_END] = RoomGameLogic.onegameend
	gameobj.stateevent[ETableState.TABLE_STATE_ONE_GAME_REAL_END] = RoomGameLogic.onegamerealend
	gameobj.stateevent[ETableState.TABLE_STATE_GAME_END] = RoomGameLogic.gameend
	gameobj.stateevent[ETableState.TABLE_STATE_CONTINUE] = RoomGameLogic.continue
	gameobj.stateevent[ETableState.TABLE_STATE_CONTINUE_AND_STANDUP] = RoomGameLogic.continue_and_standup
	gameobj.stateevent[ETableState.TABLE_STATE_CONTINUE_AND_LEAVE] = RoomGameLogic.continue_and_leave
	return true
end

function RoomGameLogic.run(gameobj)
	local f = nil
	while true do
		if gameobj.tableobj.state == ETableState.TABLE_STATE_WAIT_ALL_READY then
			break
		end

		f = gameobj.stateevent[gameobj.tableobj.state]
		if f == nil then
			break
		end
		f(gameobj)
	end
end

function RoomGameLogic.gamestart(gameobj)
	local tableobj = gameobj.tableobj
	tableobj.state = ETableState.TABLE_STATE_WAIT_PLAYER_SITDOWN
	----设置一些连续牌桌游戏的变量
end

function RoomGameLogic.onegamestart(gameobj)
	local tableobj = gameobj.tableobj
	RoomGameLogic.onegamestart_inittable(gameobj)
	tableobj.state = ETableState.TABLE_STATE_WAIT_START_COUNT_DOWN
end


function RoomGameLogic.continue(gameobj)
	local tableobj = gameobj.tableobj
	if tableobj.timer_id >= 0 then
		timer.cleartimer(tableobj.timer_id)
		tableobj.timer_id = -1
	end
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	local seat = tableobj.seats[tableobj.action_seat_index]

	local noticemsg = {
		rid = seat.rid,
		roomsvr_seat_index = tableobj.action_seat_index,
		action_type = tableobj.action_type,
		cards = {},
	}

	msghelper:sendmsg_to_alltableplayer("DoactionResultNtc", noticemsg)

	local is_end_game = false
	tableobj.state = ETableState.TABLE_STATE_WAIT_CLIENT_ACTION
end

function RoomGameLogic.continue_and_standup(gameobj)
	RoomGameLogic.continue(gameobj)
end

function RoomGameLogic.continue_and_leave(gameobj)
	
end


function RoomGameLogic.onegameend(gameobj)
	-- body
	local tableobj = gameobj.tableobj

	if tableobj.timer_id >0 then
		timer.cleartimer(tableobj.timer_id)
		tableobj.timer_id = -1
	end
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	tableobj.state = ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END
end

function RoomGameLogic.onegamerealend(gameobj)
	-- body
	local tableobj = gameobj.tableobj

	if tableobj.timer_id >0 then
		timer.cleartimer(tableobj.timer_id)
		tableobj.timer_id = -1
	end

	local noticeDoReadymsg = {
		rid = 0,
		roomsvr_seat_index = 0,
		ready_to_time = 0,
	}

	tableobj.state = ETableState.TABLE_STATE_WAIT_ALL_READY
end

function RoomGameLogic.gameend(gameobj)
	local tableobj = gameobj.tableobj
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	----重置桌子状态为等待玩家准备开始游戏的状态
	roomtablelogic.resetTable(tableobj)
end

function RoomGameLogic.onsitdowntable(gameobj, seat)
	
end

function RoomGameLogic.is_ingame(gameobj, seat)
	return true
end

function RoomGameLogic.onegamestart_inittable(gameobj)
	local tableobj = gameobj.tableobj
	tableobj.action_seat_index = 0
	tableobj.action_to_time = 0
	tableobj.action_type = 0


	if tableobj.timer_id >= 0 then
		timer.cleartimer(tableobj.timer_id)
		tableobj.timer_id = -1
	end	
end

function RoomGameLogic.standup_clear_seat(gameobj, seat)
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	roomseatlogic.clear_seat(seat)
end

return RoomGameLogic